# -*- coding: utf-8 -*-
import unittest
import sys
import os
import time

# 添加执行环境
file_exec_path = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
sys.path.insert(0, file_exec_path)
from framework_strategy.gopy.trader.engine import EmailEngine, MainEngine, EventEngine  # NOQA

# create db init
from framework_strategy.gopy.dao.orderline import *  # NOQA
from framework_strategy.gopy.driver.core import create_engine, OPRedis  # NOQA


def init_db():
    pass


class TestDao(unittest.TestCase):

    # @unittest.skip
    def test_db_init(self):
        print("[*] Db 初始化成功 ....")
        pass

    @unittest.skip
    def test_db_drop(self):
        Base.metadata.drop_all(engine)
        print("[*] Db 清除成功 ....")

    @unittest.skip
    def test_db_insert(self):

        _, db_session = create_engine()

        sm = StrategyModel(
            app_name="svr",
            user_name="test",
            note="测试策略",
        )

        db_session.add(sm)
        db_session.commit()

    @unittest.skip
    def test_db_update(self):
        _, db_session = create_engine()
        queryset = db_session.query(
            StrategyModel).filter().first()  # -> object
        print("[queryset]", queryset)

        res = db_session.query(StrategyModel).filter(StrategyModel.id == 1).update({
            "app_name": "rnn"
        })
        # commit
        db_session.commit()
        print("[res]", res)

        queryset = db_session.query(StrategyModel).filter(
            StrategyModel.id == 1).first()  # -> object
        print("[queryset]", queryset)

        # db_session.add(sm)
        # db_session.commit()
        pass

    @unittest.skip
    def test_db_del(self):

        _, db_session = create_engine()
        res = db_session.query(StrategyModel).filter(
            StrategyModel.id == 1).delete()
        db_session.commit()
        print("[res]", res)

        queryset = db_session.query(StrategyModel).filter(
            StrategyModel.id == 1).first()  # -> object
        print("[queryset]", queryset)

    @unittest.skip
    def test_db_read(self):

        _, db_session = create_engine()
        # queryset = db_session.query(StrategyModel).filter().all()
        queryset = db_session.query(
            StrategyModel).filter().first()  # -> object
        print("[queryset]", queryset)

    @unittest.skip
    def test_db_clear_all(self):

        _, db_session = create_engine()

        res = db_session.query(StrategyModel).filter().delete()
        db_session.commit()
        print("[res]", res)
        pass

    @unittest.skip
    def test_redis(self):

        opr = OPRedis()
        opr.set("opr-redis", 11111)
        print("[opr-redis]", opr.get("opr-redis"))

        desc = opr.get("describe").decode()
        print(desc)

        # ------1-------
        opr.new(db=1)
        opr.set("opr-redis", 22222222222)
        print("[opr-redis]", opr.get("opr-redis"))
        pass


if __name__ == "__main__":

    unittest.main()
    pass
